﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="NonSquareMatrixException.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Exceptions
{
    #region Usings

    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Security;

    using ExulLibrary.Mathematics.LinearAlgebra.Properties;

    #endregion

    /// <summary>
    ///     Describes an error when non-square matrix is passed as value of parameter that doesn't support it.
    /// </summary>
    [Serializable]
    public sealed class NonSquareMatrixException : ArgumentException
    {
        #region Constructors and Destructors

        #region public

        /// <summary>
        ///     Initializes a new instance of the <see cref="NonSquareMatrixException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="rowsCount">
        ///     The actual count of rows.
        /// </param>
        /// <param name="columnsCount">
        ///     The actual count of columns.
        /// </param>
        public NonSquareMatrixException(string paramName, int rowsCount, int columnsCount)
            : this(paramName, rowsCount, columnsCount, null)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="NonSquareMatrixException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="rowsCount">
        ///     The actual count of rows.
        /// </param>
        /// <param name="columnsCount">
        ///     The actual count of columns.
        /// </param>
        /// <param name="innerException">
        ///     The inner exception.
        /// </param>
        public NonSquareMatrixException(string paramName, int rowsCount, int columnsCount, Exception innerException)
            : base(Resources.NonSquareMatrix, paramName, innerException)
        {
            this.RowsCount = rowsCount;
            this.ColumnsCount = columnsCount;
        }

        #endregion

        #region private

        /// <summary>
        ///     Initializes a new instance of the <see cref="NonSquareMatrixException"/> class.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        private NonSquareMatrixException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.RowsCount = info.GetInt32("RowsCount");
            this.ColumnsCount = info.GetInt32("ColumnsCount");
        }

        #endregion

        #endregion

        #region Properties

        #region public

        /// <summary>
        ///     Gets the actual count of columns.
        /// </summary>
        /// <value>
        ///     The actual count of columns.
        /// </value>
        public int ColumnsCount
        {
            get;
            private set;
        }

        /// <summary>
        ///     Gets the actual count of rows.
        /// </summary>
        /// <value>
        ///     The actual count of rows.
        /// </value>
        public int RowsCount
        {
            get;
            private set;
        }

        #endregion

        #endregion

        #region Overridden Properties

        #region public

        /// <summary>
        ///     Gets the error message and the parameter name, or only the error message if no parameter name is set.
        /// </summary>
        /// <value>
        ///     A text string describing the details of the exception.
        /// </value>
        public override string Message
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture, base.Message, this.ParamName, this.RowsCount, this.ColumnsCount);
            }
        }

        #endregion

        #endregion

        #region Overridden Methods

        #region public

        /// <summary>
        ///     Sets the <see cref="SerializationInfo"/> object with the parameter name and additional exception information.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="info"/> is <see langword="null"/>.
        /// </exception>
        [SecurityCritical]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            info.AddValue("RowsCount", this.RowsCount);
            info.AddValue("ColumnsCount", this.ColumnsCount);
        }

        #endregion

        #endregion
    }
}